"""
@Project:education
@File:views_management.py
@Author:袁浩天
@Date:11:07
"""
from django.shortcuts import render

from user.models import *
from user.serializers import *

from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from rest_framework import status

from django.http import HttpResponse
from django.db.models import Q
from django.db.models import F

from utils.redis_middleware import redis_conn
import string
import random
import json
# user = request.session.get("user")


# 学校
class SchoolView(ViewSet):

    # 获取全部学校
    def all_school(self, request):
        try:
            # user = request.session.get("user")
            # role_id = user.get("role")
            # if int(role_id) != 2:
            #     return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            school = SchoolModel.objects.filter(is_delete=0).all()
            school_ser = SchoolSerializers(school, many=True).data
            return Response({"code" : status.HTTP_200_OK, "msg":"获取成功", "data": school_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 添加学校
    def add_school(self, request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            school_name = request.data.get("school_name")
            school = SchoolModel.objects.filter(school_name=school_name).count()

            data = {"school_name":school_name}
            ser = SchoolSerializers(data=data)
            if not ser.is_valid():
                return Response({"code":status.HTTP_400_BAD_REQUEST,"msg":ser.errors})

            if school != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该学校名称已存在"})

            SchoolModel.objects.create(school_name=school_name)

            return Response({"code" : status.HTTP_200_OK, "msg":"添加成功"})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"添加失败"})


# 学校tow
class SchoolViewTow(ViewSet):

    # 获取单个学校
    def one_school(self, request, id):
        try:
            # user = request.session.get("user")
            # role_id = user.get("role")
            # if int(role_id) != 2:
            #     return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            school = SchoolModel.objects.filter(id=id, is_delete=0).first()
            school_ser = SchoolSerializers(school).data
            return Response({"code": status.HTTP_200_OK,"msg": "获取成功","data": school_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "获取失败"})

    # 修改学校信息
    def upd_school(self, request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            school_name = request.data.get("school_name")
            school = SchoolModel.objects.filter(school_name=school_name).count()

            data = {"school_name":school_name}
            ser = SchoolSerializers(data=data)
            if not ser.is_valid():
                return Response({"code":status.HTTP_400_BAD_REQUEST,"msg":ser.errors})

            if school != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该学校名称已存在"})

            SchoolModel.objects.filter(id=id).update(school_name=school_name)

            return Response({"code" : status.HTTP_200_OK, "msg":"修改成功"})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"修改失败"})

    # 删除学校
    def del_school(self, request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            school = SchoolModel.objects.filter(id=id).update(is_delete=1)
            return Response({"code": status.HTTP_200_OK,"msg": "删除成功"})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "删除失败"})


# 班级
class ClassView(ViewSet):

    # 获取全部班级
    def all_class(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class = TheClassModel.objects.filter(is_delete=0).all()
            the_class_ser = TheClassSerializers(the_class, many=True).data
            return Response({"code" : status.HTTP_200_OK, "msg":"获取成功", "data": the_class_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 添加班级
    def add_class(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class_name = request.data.get("the_class_name")
            # school = request.data.get("school")
            user = request.session.get("user")
            school_id = user.get("school")

            # school_id = SchoolModel.objects.get(school_name=school).id

            data = {"the_class_name": the_class_name, "school": school_id}
            print("data", data)

            the_class = TheClassModel.objects.filter(the_class_name=the_class_name, school=school_id).count()

            if the_class != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该班级名称已存在"})

            ser = TheClassSerializers(data=data)

            if not ser.is_valid():
                return Response({"code":status.HTTP_400_BAD_REQUEST,"msg":ser.errors})

            TheClassModel.objects.create(the_class_name=the_class_name, school_id=school_id)


            return Response({"code" : status.HTTP_200_OK, "msg":"添加成功"})

        except Exception as e:
            print("报错", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"添加失败"})


# 班级tow
class ClassViewTow(ViewSet):

    # 获取单个班级
    def one_class(self, request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class = TheClassModel.objects.filter(id=id, is_delete=0).all()
            the_class_ser = TheClassSerializers(the_class,many=True).data
            return Response({"code": status.HTTP_200_OK,"msg": "获取成功","data": the_class_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "获取失败"})

    # 修改班级信息
    def upd_class(self, request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class_name = request.data.get("the_class_name")
            school = request.data.get("school")

            school_id = SchoolModel.objects.get(school_name=school).id

            data = {"the_class_name": the_class_name, "school": school_id}
            print("data", data)

            the_class = TheClassModel.objects.filter(the_class_name=the_class_name, school=school_id).count()

            if the_class != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该班级名称已存在"})

            ser = TheClassSerializers(data=data)

            if not ser.is_valid():
                return Response({"code":status.HTTP_400_BAD_REQUEST,"msg":ser.errors})

            TheClassModel.objects.filter(id=id).update(the_class_name=the_class_name, school_id=school_id)

            return Response({"code" : status.HTTP_200_OK, "msg":"修改成功"})

        except Exception as e:
            print("报错", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"修改失败"})

    # 删除班级
    def del_class(self,request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            TheClassModel.objects.filter(id=id).update(is_delete=1)
            return Response({"code": status.HTTP_200_OK,"msg": "删除成功"})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "删除失败"})


# 教师
class TeacherView(ViewSet):

    # 获取全部教师
    def all_teacher(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"没有权限"})
            teacher = TeacherModel.objects.filter(is_delete=0).all()
            teacher_ser = TeacherSerializers(teacher, many=True).data
            return Response({"code" : status.HTTP_200_OK, "msg":"获取成功", "data": teacher_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 添加教师
    def add_teacher(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class = request.data.get("the_class")
            user = request.data.get("user")
            print("接收", request.data)

            the_class_id = TheClassModel.objects.get(the_class_name=the_class).id
            user_id = UserModel.objects.get(user_name=user).id

            the_classes = TeacherModel.objects.filter(the_class=the_class_id, user=user_id).count()

            if the_classes != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该教师已和该班级绑定存在"})

            # data = {"the_class_id": the_class_id, "user_id": user_id}
            # print("data", data)
            # ser = TeacherSerializers(data=data)
            # if not ser.is_valid():
            #     return Response({"code":status.HTTP_400_BAD_REQUEST,"msg":ser.errors})

            TeacherModel.objects.create(the_class_id=the_class_id, user_id=user_id)

            return Response({"code" : status.HTTP_200_OK, "msg":"添加成功"})

        except Exception as e:
            print("报错", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"添加失败"})


# 教师tow
class TeacherViewTow(ViewSet):

    # 获取单个教师
    def one_teacher(self,request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            teacher = TeacherModel.objects.filter(id=id).all()
            teacher_ser = TeacherSerializers(teacher, many=True).data
            return Response({"code" : status.HTTP_200_OK, "msg":"获取成功", "data": teacher_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 修改教师信息
    def upd_teacher(self,request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class = request.data.get("the_class")
            user = request.data.get("user")
            print("接收", request.data)

            the_class_id = TheClassModel.objects.get(the_class_name=the_class).id
            user_id = UserModel.objects.get(user_name=user).id

            the_classes = TeacherModel.objects.filter(the_class=the_class_id, user=user_id).count()

            if the_classes != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST, "msg":"该教师已和该班级绑定存在"})

            # data = {"the_class": the_class_id, "user": user_id}
            # print("data", data)
            # ser = TeacherSerializers(data=data)
            #
            # if not ser.is_valid():
            #     return Response({"code":status.HTTP_400_BAD_REQUEST,"msg":ser.errors})

            TeacherModel.objects.filter(id=id).update(the_class_id=the_class_id, user_id=user_id)

            return Response({"code" : status.HTTP_200_OK, "msg":"修改成功"})

        except Exception as e:
            print("报错", e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"修改失败"})

    # 删除教师
    def del_teacher(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 2:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            TeacherModel.objects.filter(id=id).update(is_delete=1)
            return Response({"code": status.HTTP_200_OK,"msg": "删除成功"})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "删除失败"})


# 学生
class StudentView(ViewSet):

    # 获取全部学生
    def all_student(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 3:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            student = StudentsModelModel.objects.all()
            student_ser = StudentsSerializers(student, many=True).data
            return Response({"code" : status.HTTP_200_OK, "msg":"获取成功", "data": student_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 添加学生
    def add_student(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 3:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class = request.data.get("the_class")
            user = request.data.get("user")
            print("接收",request.data)

            the_class_id = TheClassModel.objects.get(the_class_name=the_class).id
            user_id = UserModel.objects.get(user_name=user).id

            students = StudentsModelModel.objects.filter(the_class=the_class_id,user=user_id).count()

            if students != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "该学生已和该班级绑定存在"})

            StudentsModelModel.objects.create(the_class_id=the_class_id,user_id=user_id)

            # data = {"the_class": the_class_id,"user": user_id}
            # print("data",data)
            # ser = StudentsSerializers(data=data)
            # if not ser.is_valid():
            #     return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": ser.errors})

            return Response({"code": status.HTTP_200_OK,"msg": "添加成功"})

        except Exception as e:
            print("报错",e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "添加失败"})


# 学生tow
class StudentViewTow(ViewSet):

    # 获取单个学生
    def one_student(self,request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 3:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            student = StudentsModelModel.objects.filter(id=id).all()
            student_ser = StudentsSerializers(student,many=True).data
            return Response({"code": status.HTTP_200_OK,"msg": "获取成功","data": student_ser})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "获取失败"})

    # 修改学生信息
    def upd_student(self,request, id):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 3:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            the_class = request.data.get("the_class")
            user = request.data.get("user")
            print("接收",request.data)

            the_class_id = TheClassModel.objects.get(the_class_name=the_class).id
            user_id = UserModel.objects.get(user_name=user).id

            students = StudentsModelModel.objects.filter(the_class=the_class_id,user=user_id).count()

            if students != 0:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "该学生已和该班级绑定存在"})

            StudentsModelModel.objects.filter(id=id).update(the_class_id=the_class_id,user_id=user_id)

            # data = {"the_class": the_class_id,"user": user_id}
            # print("data",data)
            # ser = StudentsSerializers(data=data)
            # if not ser.is_valid():
            #     return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": ser.errors})

            return Response({"code": status.HTTP_200_OK,"msg": "修改成功"})

        except Exception as e:
            print("报错",e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "修改失败"})

    # 删除学生
    def del_student(self,request):
        try:
            user = request.session.get("user")
            role_id = user.get("role")
            if int(role_id) != 3:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "没有权限"})

            StudentsModelModel.objects.filter(id=id).update(is_delete=1)
            return Response({"code": status.HTTP_200_OK,"msg": "删除成功"})

        except Exception as e:
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR,"msg": "删除失败"})


# 用户信息
class UserView(APIView):

    # 获取所有用户
    def get(self, request):
        try:
            school_id = request.data.get("school_id")
            role_id = int(request.data.get("role_id"))
            print("接收",request.data)

            if role_id == 1:
                user = UserModel.objects.filter().all()
                user_ser = UserSerializersTow(user,many=True).data
                return Response({"code": status.HTTP_200_OK,"msg": "获取成功","data": user_ser})
            else:
                role_id += 1
                if role_id == 5:
                    return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "该用户没有权限查看全部"})
                print("角色id",role_id)
                user = UserModel.objects.filter(school_id=school_id,role=role_id).all()
                user_ser = UserSerializers(user,many=True).data
                return Response({"code": status.HTTP_200_OK,"msg": "获取成功","data": user_ser})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 添加用户
    def post(self, request):
        try:
            school_id = request.data.get("school_id")
            role_id = int(request.data.get("role_id"))
            user_name = request.data.get("user_name")
            user_gender = request.data.get("user_gender")
            password = request.data.get("password")
            user_age = request.data.get("user_age")
            user_mobile = request.data.get("user_mobile")
            print("接收",request.data)
            role_id += 1
            if role_id == 5:
                return Response({"code": status.HTTP_400_BAD_REQUEST,"msg": "该用户没有权限添加用户"})
            print("角色id",role_id)

            user = UserModel.objects.filter(school_id=school_id, user_name=user_name).count()
            if user != 0:
                return Response({"code": status.HTTP_200_OK, "msg":"该学生已存在"})

            UserModel.objects.create(school_id=school_id,role_id=role_id, user_name=user_name, user_gender=user_gender
                                            , password=password, user_age=user_age, user_mobile=user_mobile)
            return Response({"code": status.HTTP_200_OK,"msg": "添加成功"})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"添加失败"})


class UserViewTow(ViewSet):

    # 查询单个用户信息
    def one_user(self, request, id):
        try:
            user = UserModel.objects.filter(id=id).all()
            user_ser = UserSerializers(user,many=True).data
            return Response({"code": status.HTTP_200_OK,"msg": "获取成功","data": user_ser})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"获取失败"})

    # 删除
    def del_user(self, request, id):
        try:
            UserModel.objects.filter(id=id).update(is_delete=1)
            return Response({"code": status.HTTP_200_OK,"msg": "删除成功"})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"删除"})

    # 修改用户
    def upd_user(self, request, id):
        try:
            school_id = request.data.get("school_id")
            role_id = request.data.get("role_id")
            user_name = request.data.get("user_name")
            user_gender = request.data.get("user_gender")
            password = request.data.get("password")
            user_age = request.data.get("user_age")
            user_mobile = request.data.get("user_mobile")
            user_img = request.data.get("user_img")
            print("接收",request.data)
            print("角色id",role_id)

            UserModel.objects.filter(id=id).update(school_id=school_id,role_id=role_id, user_name=user_name, user_gender=user_gender
                                            , password=password, user_age=user_age, user_mobile=user_mobile, user_img=user_img)
            return Response({"code": status.HTTP_200_OK,"msg": "修改成功"})
        except Exception as e:
            print(e)
            return Response({"code": status.HTTP_500_INTERNAL_SERVER_ERROR, "msg":"修改失败"})



































